package com.tledu.spring06.dao;

import com.tledu.spring06.model.User;
import com.tledu.spring06.model.UserRowMapper;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Antg
 * @date 2021/8/10  11:01
 */
@Component
@AllArgsConstructor
public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate;
    private UserRowMapper userRowMapper;

    @Override
    public List<User> findAll() {
        List<User> userList = jdbcTemplate.query("select * from user2", userRowMapper);
        return userList;
    }

    @Override
    public User findById(int id) {
        User user = jdbcTemplate.queryForObject("select * from user2 where id = ?", userRowMapper, id);
        return user;
    }

    @Override
    public int add(User user) {
        return jdbcTemplate.update("insert into user2(username, password, nickname, age, birthday) VALUE (?,?,?,?,?)", user.getUsername(), user.getPassword(), user.getNickname(), user.getAge(), user.getBirthday());
    }

    @Override
    public int edit(User user) {
        return jdbcTemplate.update("update user2 set username = ?,password = ?,nickname = ?, age = ?,birthday = ? where id = ?", user.getUsername(), user.getPassword(), user.getNickname(), user.getAge(), user.getBirthday(), user.getId());
    }

    @Override
    public int del(int id) {
        return jdbcTemplate.update("delete from user2 where id = ?", id);
    }
}
